package soot.we.android.logic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import soot.Body;
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.Unit;
import soot.ValueBox;
import soot.jimple.Stmt;
import soot.toolkits.graph.ExceptionalUnitGraph;
import soot.toolkits.graph.UnitGraph;
import soot.we.android.base.ComponentClass;

public class ProcessJimpleClasses {
	
	
	
	  /**
     * CallbackMethod including the AndroidLifeCycleMethod and 
     */
    public void collectCallbackMethod(ComponentClass tempCompClass){
    	
    	   collectCallbackMethodfromLayout();
    	   collectCallbackMethodfromComponentClass();
    	  
    }
    public void collectCallbackMethodfromComponentClass() {
		
		
	}
	public void collectCallbackMethodfromLayout(){
    	
    }
	public List<String> createDummySequence(ComponentClass tempCompClas) {
		return null;
	}

	public static UnitGraph getCfgFromMethod(Body body) {
		ArrayList<String> unitList = new ArrayList<String>();
		SootMethod method = body.getMethod();
		System.out.println("instrumenting method : " + method.getName());
		UnitGraph g = new ExceptionalUnitGraph(body);

		return g;
	}

	public void searchInvoke(Body body) {
		// **********find Static invoke***************
		Iterator<Unit> iter = body.getUnits().snapshotIterator();
		while (iter.hasNext()) {
			Stmt s = (Stmt) iter.next();
			if (s.containsInvokeExpr()) {
				String declaringClass = s.getInvokeExpr().getMethod()
						.getDeclaringClass().getName();
				String name = s.getInvokeExpr().getMethod().getName();

				System.out.println("declaringClass: " + declaringClass);
				System.out.println("name: " + name);

				if (declaringClass.equals("android.content.Intent")) {
					for (ValueBox ss : s.getUseBoxes()) {
						System.out.println("declaringClass's values : "
								+ ss.getValue().toString());
					}
				}
			}
		}
	}

	public static List<String> findAnonyInnerClass(String Outerclassname) {
		List<String> outClassList = new ArrayList<String>();
		Iterator<SootClass> iterator = Scene.v().getApplicationClasses()
				.iterator();
		while (iterator.hasNext()) {
			String classname = iterator.next().getName();
			if (classname.contains(Outerclassname)
					&& !classname.equals(Outerclassname)) {
				System.out.println("Anonyinnerclass :" + classname);
				outClassList.add(classname);
			}
		}
		return outClassList;

	}
	
	

}
